/*
 * WantToBuyModel.java
 * business
 *
 * Created by chentao on 2017/5/15.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import com.yidiandao.business.constant.Common;
import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.OfferEntity;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.net.BaseResponse;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.data.request.WantBuyOperateRequest;
import com.yidiandao.business.data.response.DiscoveryWantToBuyDetailHeaderResponse;
import com.yidiandao.business.data.response.WantToBuyDetailOfferListResponse;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.Functional;
import com.yidiandao.business.ui.widget.listview.displaylistitem.WantToBuyDetailGoodsListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.WantToBuyDetailOfferListItem;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 求购Model
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017/5/15 0015 9:17
 */
public class WantToBuyModel implements IWantToBuyModel {

    private NetBiz mNetBiz = new NetBiz();

    private int mPage;

    private List<DisplayListItem> mOfferListItems = new ArrayList<>();

    @Override
    public void submitOffer(String tag, String wantId, String price, String shippingFee, String mobile, final OnNetListener<Object> listener) {

        WantBuyOperateRequest request = new WantBuyOperateRequest();
        request.setUrl(Urls.SUBMIT_OFFER);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        request.setWantId(wantId);
        request.setPrice(price);
        request.setShippingFee(shippingFee);
        request.setMobile(mobile);

        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess(null);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed(netErrorInfo.getMessage());
                }
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed(bizErrorInfo.getMessage());
                }
                return false;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void getWantToBuyDetail(final String wantId, final String tag, final OnWantToBuyDetailListener listener) {

        WantBuyOperateRequest request = new WantBuyOperateRequest();
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.DISCOVERY_WANT_TO_BUY_INFO);
        request.setTag(tag);
        request.setWantId(wantId);

        // 请求头部信息
        mNetBiz.sendGetRequest(request, DiscoveryWantToBuyDetailHeaderResponse.class, new NetListener<DiscoveryWantToBuyDetailHeaderResponse>() {
            @Override
            public void onSuccess(DiscoveryWantToBuyDetailHeaderResponse response) {
                if (listener != null) listener.onHeader(response.getData().getWantInfo());
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) listener.onFailed();
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) listener.onFailed();
                return false;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void getWantToBuyOfferList(final boolean isRefreshing, final List<Goods> similarGoodsList, final String wantId, String tag, final OnOfferListListener listener) {

        if (isRefreshing) {
            mPage = 1;
        }

        WantBuyOperateRequest request = new WantBuyOperateRequest();
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.DISCOVERY_WANT_TO_BUY_OFFER_LIST);
        request.setTag(tag);
        request.setWantId(wantId);
        request.setPageSize(Common.PAGER_SIZE);
        request.setPage(mPage);

        mNetBiz.sendGetRequest(request, WantToBuyDetailOfferListResponse.class, new NetListener<WantToBuyDetailOfferListResponse>() {
            @Override
            public void onSuccess(WantToBuyDetailOfferListResponse response) {
                mPage++;

                if (isRefreshing) {
                    mOfferListItems.clear();
                    if (similarGoodsList != null && similarGoodsList.size() != 0) {
                        List<DisplayListItem> similarListItems = Functional.serverToClient(similarGoodsList, new Functional.Processor<Goods, DisplayListItem>() {
                            @Override
                            public DisplayListItem processToClient(Goods goods) {
                                return new WantToBuyDetailGoodsListItem(goods);
                            }
                        });
                        mOfferListItems.addAll(similarListItems);
                    }
                }

                List<OfferEntity> offerList = response.getData().getOfferList();

                List<DisplayListItem> listItems = Functional.serverToClient(offerList, new Functional.Processor<OfferEntity, DisplayListItem>() {
                    @Override
                    public DisplayListItem processToClient(OfferEntity offerEntity) {
                        return new WantToBuyDetailOfferListItem(offerEntity);
                    }
                });
                mOfferListItems.addAll(listItems);

                if (listener != null)
                    listener.onSuccess(mOfferListItems, listItems.size() == 0, response.hasMoreData());
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) listener.onFailed();
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) listener.onFailed();
                return false;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void cancelOffer(String tag, String offerId, final OnNetListener<Object> listener) {
        WantBuyOperateRequest request = new WantBuyOperateRequest();
        request.setTag(tag);
        request.setOfferId(offerId);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.CANCEL_OFFER);

        mNetBiz.sendGetRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess(null);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed(null);
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed(null);
                }
                return true;
            }

            @Override
            public void onFinal() {

            }
        });
    }
}
